//Socket
#include <arpa/inet.h>
#include <sys/shm.h>
#include<errno.h>

#include <netinet/in.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>


#include <iostream>   	//C++相关的头文件
#include <sstream>	//与fstream类型类似，头文件sstream中定义的类型都继承iostream头文件中定义的类型
//#include <string>   	//这里的string是c++中string类，包含s.size()等操作，但也包含如strcpy等函数，相当于内部引用了string.h，所以用了string就可以不用string.h


//mkdir
#include <sys/stat.h>

#include <stdio.h>    	//C相关的头文件
#include <time.h>     	//使用time(NULL)时需要调用的头文件
#include <ctime>	//为了使用日期和时间相关的函数和结构体,需要在C++程序中引用ctime头文件
#include <string.h>   	//包含比如strcpy、strcat之类的字符串处理函数
#ifdef _WIN32
#include <windows.h>		//windows下Sleep头文件
#elif defined(__linux__) || defined(__APPLE__)
#include <unistd.h>		//linux下sleep头文件
#endif

#define ISAPI_OUT_LEN 3*1024*1024
#define ISAPI_STATUS_LEN 8*1024	

#include "HCNetSDK.h"   //海康sdk最重要的头文件

using namespace std;

void CALLBACK GetThermInfoCallback_test(DWORD dwType, void* lpBuffer, DWORD dwBufLen, void* pUserData);

static NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0}; //device info
int main()
{
	DWORD dwChannel = 1;  //热成像通道      
    //---------------------------------------     
    //初始化     
    NET_DVR_Init();      
    //设置连接时间与重连时间     
    NET_DVR_SetConnectTime(2000, 1);     
    NET_DVR_SetReconnect(10000, true);      
    //---------------------------------------     
    //注册设备(监听报警可以不注册)     
    LONG lUserID;     
    NET_DVR_DEVICEINFO_V30 struDeviceInfo;     
    lUserID = NET_DVR_Login_V30("192.168.1.64", 8000, "admin", "abcd1234", &struDeviceInfo);     
    if (lUserID < 0)     
    {
        printf("Login error, %d\n", 
        NET_DVR_GetLastError());         
        NET_DVR_Cleanup();          
    return 0;     
    }      
    //启动实时温度检测     
    NET_DVR_REALTIME_THERMOMETRY_COND struThermCond = {0};     
    struThermCond.dwSize = sizeof(struThermCond);     
    struThermCond.byRuleID = 0;       //规则ID，0代表获取全部规则，具体规则ID从1开始     
    struThermCond.dwChan = dwChannel; //从1开始，0xffffffff代表获取全部通道     
    LONG lHandle = NET_DVR_StartRemoteConfig(lUserID, NET_DVR_GET_REALTIME_THERMOMETRY, &struThermCond, sizeof(struThermCond), GetThermInfoCallback_test, NULL); 
    if (lHandle < 0)     
    {         
        printf("NET_DVR_GET_REALTIME_THERMOMETRY failed, error code: %d\n", NET_DVR_GetLastError());     
    }     
    else     
    {         
        printf("NET_DVR_GET_REALTIME_THERMOMETRY is successful!");     
    }      
    sleep(3);  //等待一段时间，接收实时测温结果      
    //关闭长连接配置接口所创建的句柄，释放资源     
    if(!NET_DVR_StopRemoteConfig(lHandle))     
    {        
         printf("NET_DVR_StopRemoteConfig failed, error code: %d\n", NET_DVR_GetLastError());     
    }     
    //注销用户，如果前面没有登录，该步骤则不需要     
    NET_DVR_Logout(lUserID);      //释放SDK资源     
    NET_DVR_Cleanup();
    return 0;


}


void CALLBACK GetThermInfoCallback_test(DWORD dwType, void* lpBuffer, DWORD dwBufLen, void* pUserData) 
{  
    return;   
	if (dwType == NET_SDK_CALLBACK_TYPE_DATA)     {         
		LPNET_DVR_THERMOMETRY_UPLOAD lpThermometry = new NET_DVR_THERMOMETRY_UPLOAD;         
		memcpy(lpThermometry, lpBuffer, sizeof(*lpThermometry));

			
		if(lpThermometry->byRuleCalibType==0) //点测温         
		{             
			printf("点测温信息:fTemperature[%f]\n", lpThermometry->struPointThermCfg.fTemperature);  
    
		}           
		if((lpThermometry->byRuleCalibType==1)||(lpThermometry->byRuleCalibType==2)) //框/线测温         
		{             
			printf("框/线测温信息:\n");         
			
		}                 
		if (lpThermometry != NULL)         
		{             
			delete lpThermometry;             
			lpThermometry = NULL;         
		}     


	}   //if
	else if (dwType == NET_SDK_CALLBACK_TYPE_STATUS)     
	{         
		DWORD dwStatus = *(DWORD*)lpBuffer;         
		if (dwStatus == NET_SDK_CALLBACK_STATUS_SUCCESS)         
		{             
			printf("dwStatus:NET_SDK_CALLBACK_STATUS_SUCCESS\n");                     
		}         
		else if (dwStatus == NET_SDK_CALLBACK_STATUS_FAILED)         
		{             
			DWORD dwErrCode = *(DWORD*)((char *)lpBuffer + 4);             
			printf("NET_DVR_GET_MANUALTHERM_INFO failed, Error code %d\n", dwErrCode);         
		}     
	} 
} 